package FillMythMetadataHttpCaller;
use strict;

#global variables
my $global_user_agent = LWP::UserAgent->new;
$global_user_agent->timeout(15); 
#this is for HTTP debuging.
#$global_user_agent->show_progress(1);

sub get_global_agent
{
	return $global_user_agent;
}

sub get_http_response_lwp
{
	my $request_url = $_[0];
	print "About to call GET HTTP url: '$request_url'\n";
	my $req = HTTP::Request->new(GET => $request_url);

	# Pass request to the user agent and get a response back
	my $res = $global_user_agent->request($req);

	# Check the outcome of the response
	if ($res->is_success) 
	{
		my $response_content = $res->content;
		#print "Got HTTP response:\n".$response_content."\n";
		return $response_content;
	}
	else 
	{
		print "Failed to get response! Status '".$res->status_line."'\n";
		return "";
	}
}

sub get_http_response_wget
{
	my $request_url = $_[0];
	print "About to call WGET HTTP url: '$request_url'\n";
	my $target_file = "/tmp/myth_video_filler_resp.html";
	system("wget ".$request_url." -O ".$target_file);
	
	open FILE, $target_file; 
	my $string = join("", <FILE>); 
	close FILE;
	
	return $string;
}

my %urls_cache = ();
sub get_http_response
{
	my $url = $_[0];
	#checking if URL is cached already
	my $cachedContect = $urls_cache{$url};
	if ($cachedContect)
	{
		#print "'".$url."' is cached.\n";
		return $cachedContect;
	}
	print "'".$url."' is not cached.\n";
	my $content = get_http_response_lwp($url);
	$content =~ s/(\n|\r|\f)/ /gi;
	$urls_cache{$url} = $content;
	return $content;
}

#functions
sub convert_for_url
{
	my $show_name = $_[0];
	$show_name =~ s/ /%20/gi;
	
	return $show_name;
}

my $poster_download_last_url = '';
my $poster_download_last_target = '';
sub download_poster
{
	my $poster_url = $_[0];
	my $target = $_[1];
	
	my $is_this_motech = $poster_url =~ m/motechposters/;
	my $motech_referer = 0;
	
	if ($is_this_motech) {
		$motech_referer = $poster_url;
		$motech_referer =~ s/http:\/\/www.motechposters.com\/posters\//http:\/\/www.motechposters.com\/title\//;
		$motech_referer =~ s/_poster.jpg/\//;
	}
	
	#downloading or local copy?
	if ($poster_download_last_url eq $poster_url)
	{
		print "No need to download $poster_url. I have it locally at $poster_download_last_target.\n";
		system("cp '$poster_download_last_target' '$target'");
		return 1;
	}
	else
	{
		my $return_code;
		if ($motech_referer) {
		my @args = ('wget',$poster_url,'-O',$target,'-U',$global_user_agent->agent,'--referer',$motech_referer);
		print "Downloading $poster_url with referer $motech_referer to $target\n";
		$return_code = system( @args );
		} else {
		my @args = ('wget',$poster_url,'-O',$target,'-U',$global_user_agent->agent);
		print "Downloading $poster_url to $target\n";
		$return_code = system( @args );
		}
		my $this_poster_filesize = -s $target;
		if (!$this_poster_filesize) {$this_poster_filesize = 0;}
		if (($return_code eq 0) && ($this_poster_filesize != 0))
		{
			$poster_download_last_url = $poster_url;
			$poster_download_last_target = $target;
			return 1;
		}
		else
		{
			if ($this_poster_filesize == 0) {
			unlink($target);
			print "Poster retrieved is zero bytes!\n";
			return 0;
			}
			else 
			{
			print "Failed to download poster! Return code: ".$return_code."\n";
			return 0;
			}
		}
	}
}

1;